<?php
class categories extends Admin_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	
	function index($id = FALSE)
	{
		if($_POST)
		{
			foreach($_POST['sequence'] as $key => $sequence)
			{
				$category = new Category($key);
				$category->sequence = $sequence;
				$category->save();
			}
			redirect($_SERVER['HTTP_REFERER']);
		}
		$data['categories'] = new Category();
		if($id)
		{
			$data['categories']->where('parent_id',$id)->order_by('layout_id asc');
			$data['parent'] = new Category($id);
		}
		else
		{
			$data['categories']->where('parent_id','0');
		}
		$data['categories']->get_page(50);
		//$data['categories']->check_last_query();
		$this->template->build('admin/category_index',$data);
	}
	
	function form($parent_id = 0, $id=FALSE)
	{	
		$data['structure'] = new Structure();
		if($parent_id == 0) $data['structure']->where('parent_id', $parent_id); else $data['structure']->where('parent_id', 1);
		$data['category'] = new Category();
		$data['category']->get_by_id($id);
		if($parent_id)$data['parent'] = new Category($parent_id);
		$this->template->build('admin/category_form',$data);
	}
	
	function save($id=FALSE)
	{
		if($_POST)
		{
			$category = new Category($id);
			$category->from_array($_POST);
			$category->save();
			set_notify('success', lang('save_data_complete'));
		}
		if(isset($_POST['parent_id']))
		{
			redirect('contents/admin/categories/index/'.$_POST['parent_id']);
		}
		else
		{
			redirect('contents/admin/categories/');
		}
	}
	
	function delete($id)
	{
		if($id)
		{
			$category = new Category($id);
			$module = $category->module;
			$category->delete();
			set_notify('success', lang('delete_data_complete'));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function menu($id = 0)
	{
		$data['categories'] = new Category();
		if($id == 0)
		{
			$data['categories']->where_related_structure('type', 'agency');
		}
		$data['categories']->where('parent_id', $id)->get();
		
		if($data['categories']->exists())
		{
			$this->load->view('admin/category_menu',$data);
		}
	}
	
	function menu285()
	{
		$data['categories'] = new Category();
		$data['categories']->where_related_structure('type', 'agency');
		$data['categories']->where_in('id', array(105,132,162,185,204,232,250,285))->get();
		
		if($data['categories']->exists())
		{
			$this->load->view('admin/category_menu',$data);
		}
	}
	
}
?>